package com.itheima.controller;

import cn.hutool.core.collection.CollUtil;
import com.itheima.domain.PmsProductAttribute;
import com.itheima.domain.PmsProductCategoryAttributeRelation;
import com.itheima.service.db.PmsProductAttributeCategoryService;
import com.itheima.service.db.PmsProductAttributeService;
import com.itheima.service.db.PmsProductCategoryAttributeRelationService;
import com.itheima.util.R;
import com.itheima.vo.PmsAttributeCategoryVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/productAttribute")
public class PmsProductAttributeController {
    @DubboReference
    PmsProductAttributeService ppaService;
    @DubboReference
    PmsProductCategoryAttributeRelationService ppcarService;
    @DubboReference
    PmsProductAttributeCategoryService ppacService;

    //根据商品分类的id获取商品属性及属性分类
    @GetMapping("/attrInfo/{productCategoryId}")
    public R attrInfo(@PathVariable Long productCategoryId) {
        //从关系表查询分类和属性的关系
        List<PmsProductCategoryAttributeRelation> ppcars = ppcarService.findByCategoryId(productCategoryId);
        List<PmsAttributeCategoryVo> vos = new ArrayList<>();
        if (CollUtil.isNotEmpty(ppcars)) {
            //获取属性id集合并去查询属性集合
            List<Long> ids = CollUtil.getFieldValues(ppcars, "productAttributeId", Long.class);
            List<PmsProductAttribute> ppas = ppaService.findByIds(ids);
            //遍历属性集合获取属性分类集合
            for (PmsProductAttribute ppa : ppas) {
                PmsAttributeCategoryVo vo = new PmsAttributeCategoryVo();
                vo.setAttributeId(ppa.getId().intValue());
                vo.setAttributeCategoryId(ppa.getProductAttributeCategoryId().intValue());
                vos.add(vo);
            }
        }
        return R.success(vos);
    }

    //根据分类查询属性列表或参数列表
    @GetMapping("/list/{cid}")
    public R findByTtpe(
            @PathVariable Long cid, //属性分类id
            @RequestParam Integer type, //属性种类
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return R.success(ppaService.findByTtpe(cid, type, pageNum, pageSize));
    }

    //添加商品属性信息
    @PostMapping("/create")
    public R create(@RequestBody PmsProductAttribute pmsProductAttribute) {
        ppaService.save(pmsProductAttribute);
        //判断属性数量+1还是参数数量+1
        ppacService.add(pmsProductAttribute.getType(), pmsProductAttribute.getProductAttributeCategoryId());
        return R.success("添加成功");
    }

    //批量删除商品属性
    @PostMapping("/delete")
    public R delete(@RequestParam Long[] ids) {
        //判断属性数量-1还是参数数量-1
        PmsProductAttribute ppa = ppaService.findById(ids[0]);
        ppacService.subtract(ppa.getType(), ppa.getProductAttributeCategoryId(), ids.length);
        ppaService.deleteByIds(ids);
        return R.success("删除成功");
    }

    //修改商品属性信息
    @PostMapping("/update/{id}")
    public R update(@PathVariable Long id, @RequestBody PmsProductAttribute pmsProductAttribute) {
        PmsProductAttribute ppa = ppaService.findById(id);
        //判断是否更换了类型
        Long ppacId = pmsProductAttribute.getProductAttributeCategoryId();
        Long ppacIdOld = ppa.getProductAttributeCategoryId();
        if (ppacIdOld != ppacId) {
            ppacService.add(ppa.getType(), ppacId);
            ppacService.subtract(ppa.getType(), ppacIdOld, 1);
        }
        ppaService.update(pmsProductAttribute);
        return R.success("修改成功");
    }

    //查询单个商品属性
    @GetMapping("/{id}")
    public R findById(@PathVariable Long id) {
        return R.success(ppaService.findById(id));
    }
}
